clear
use "$sourcedatadir/ca/CES72s_F1.dta"

/* 1972 */

numlabel, add

genl year=1972

genl idnum=RESPID1A

drop if QUEB_SAM>=8

genl vote_ce=Q20Z
recode vote_ce 2 = 1 1 3 4 .a = 0

genl vote_gov=vote_ce

genl pid_ce=Q23Z
recode pid_ce 2 = 1 1 3 4 .a = 0

genl pid_gov=pid_ce

genl nopid=Q23Z
recode nopid .a = 1 1 2 3 4 = 0

genl income=Q34Z
genl income3=income
centile income, c(33 66 100)
replace income3=0 if income<=r(c_1)
centile income, c(33 66 100)
replace income3=1 if income>r(c_1) & income<=r(c_2)
centile income, c(33 66 100)
replace income3=2 if income>r(c_2) & income<=r(c_3)

save "$gendatadir/ca_CES72s_F1_processed.dta", replace

clear
/* Pooling 72 with 65-11 */
use "$sourcedatadir/ca/CES65-11_narrow.dta"
append using "$gendatadir/ca_CES72s_F1_processed.dta", keep(year idnum pid_ce pid_gov vote_ce vote_gov income3)

numlabel, add

replace income3=0 if income==0
replace income3=1 if income==.5
replace income3=2 if income==1

la de income3 0 "Low" 1 "Middle" 2 "High"
la val income3 income3

local year "1965 1968 1972 1974 1979 1980 1984 1988 1993 1997 2000 2004 2006 2008 2011"
local incmbt "1 1 1 1 1 2 1 2 2 1 1 1 1 2 2"
forvalues i=1(1)15 {
	local yr: word `i' of `year'
	local inc: word `i' of `incmbt'
	display `yr' " " `inc'
	replace vote_ce=1 if fedvote==`inc' & year==`yr'
	replace vote_ce=0 if fedvote~=`inc' & year==`yr' & fedvote~=.
	replace vote_gov=1 if fedvote==`inc' & year==`yr'
	replace vote_gov=0 if fedvote~=`inc' & year==`yr' & fedvote~=.
}

la var vote_ce "Incumbent vote (PM's party only)"
la var vote_gov "Incumbent vote (All govt parties)"

local year "1965 1968 1972 1974 1979 1980 1984 1988 1993 1997 2000 2004 2006 2008 2011"
local incmbt "1 1 1 1 1 2 1 2 2 1 1 1 1 2 2"
forvalues i=1(1)15 {
	local yr: word `i' of `year'
	local inc: word `i' of `incmbt'
	display `yr' " " `inc'
	replace pid_ce=1 if pid==`inc' & year==`yr'
	replace pid_ce=0 if pid~=`inc' & year==`yr' & pid~=.
	replace pid_gov=1 if pid==`inc' & year==`yr'
	replace pid_gov=0 if pid~=`inc' & year==`yr' & pid~=.
}

la var pid_ce "Incumbent PID (PM's party only)"
la var pid_gov "Incumbent PID (All govt parties)"

replace natlweight=1 if year==1972

genl nopid=pid
recode nopid 0=1 1/7=0 .=.

la var nopid "No PID (vs. PID)"

/* Merging nopid for 1972 */

merge 1:1 year idnum using "$gendatadir/ca_CES72s_F1_processed.dta", update keepus(nopid) nogen


save "$gendatadir/ca_cdf.dta", replace
